<?php
/**
 * Created by PhpStorm.
 * User: 17274
 * Date: 2018/3/27
 * Time: 21:06
 */
namespace app\admin\model;

use think\Model;
use think\Validate;
use think\Config;

class IndexModel extends  Model
{
    protected $table = 'yui_index';
    /*
     * rule
     * 验证data
     * @params $data
     * 新增和修改时验证
     * */
    public function validate_model($scene){
        $rule = [
            'index_wel' => 'require',
            'index_motto' => 'require',
            'index_about' => 'require',
            'status' => 'number'
        ];
        $msg = [
            'index_wel.require' => '欢迎语不能为空!',
            'index_motto.require' => '格言内容不能为空!',
            'index_about.require' => '关于本站不能为空!',
            'status.number' => '标签状态必须为数字类型!',
        ];
        $data=input('post.');
        $validate = new Validate($rule, $msg);
        $result = $validate->check($data);
        if(!$result){
            return [
                'code'=>0,
                'text'=> $validate->getError()
            ];
        }
        return ['code'=>1];
    }

    /*
    * 模块获取器
    * user_id
    * 驼峰式规则
    * */
    public function getUserIdAttr($value){
        $res=(new AdminModel())->listAll()->toArray();
        foreach ($res as $re)
            if ($re['id']==$value)
                return $re['username'];
    }

    /*
   * 根据ID获取文章信息
   * */
    public function getById($id){
        return $this->where(["id"=>$id])->find();
    }


    /*
     * 文章列表
     * */
    public function listAll(){
       return $this->order('updated_at desc')->select();
    }

    /*
     * 启用禁用列表
     * */
    public function listOnOff($status){
        $data=$this->where('status',$status)->order('updated_at desc')->select();
        return $data;
    }

    /*
     * 新增标签
     * */
    public function add(){
        $data=input('post.');
        $data['created_at']=time();
        $data['updated_at']=time();
        $data['user_id']=$_SESSION["user_id"];
        if($this->save($data)){
            $params=[
                Config::get('op_type')[0],
                Config::get('op_module')[9],
                Config::get('op_text')[10],
                $_SESSION["username"]
            ];
            tag('logs_db',$params);
            return op_success();
        }
        return op_fail();
    }

    /*
     * 修改标签
     * */
    public function edit(){
        $data=input('post.');
        $data['updated_at']=time();
        $data['user_id']=$_SESSION["user_id"];
        $id=input('post.id/d');
        if($this->where('id',$id)->update($data)){
            $params=[
                Config::get('op_type')[1],
                Config::get('op_module')[9],
                Config::get('op_text')[10],
                $_SESSION["username"]
            ];
            tag('logs_db',$params);
            return op_success();
        }
        return op_fail();
    }

    /*
     * 删除标签
     * */
    public function del(){
        $ids=input('post.ids/a');
        if ($this->destroy($ids)){
            $params=[
                Config::get('op_type')[2],
                Config::get('op_module')[9],
                Config::get('op_text')[10],
                $_SESSION["username"]
            ];
            tag('logs_db',$params);
            return op_success();
        }
        return op_fail();
    }

}